import urllib.request
import json
import jsonpath

# https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1705909254773_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true

if __name__ == '__main__':
    url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1705909254773_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'
    headers = {
        # ':authority':'dianying.taobao.com',
        # ':method':'GET',
        # ':path':'/cityAction.json?activityId&_ksTS=1705909254773_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true',
        # ':scheme':'https',
        'Accept':'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
        # 'Accept-Encoding':'gzip, deflate, br',
        'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Bx-V':'2.5.10',
        'Cookie':'cna=OZpDGl2f2l8CAXW9w2vtMZ4x; t=7852143001f043d64b40a521d3b7bff8; cookie2=1550d1e4480bf97d07ce136d6cae6a02; v=0; _tb_token_=ea5e73856debe; xlly_s=1; tb_city=110100; tb_cityName="sbG+qQ=="; tfstk=e2Le3OVA723F7LFXYGbPbTVeEF7dzZHjzU65ZQAlO9XneH_y4B9FVpOlFaJyULWCqvQBzd7p5Ha5dvQyZK_rhxgjlBddJaDjhRNwPC_-TOtTpqOp95VUEjtilz-VXZsv31yahmjonIXM7C-l0s23nO4ure0RYtyOB1C97AsdnBYUrzVdTMWDtOIrI8CGM48JY8qPx1CNhflN-3u1QKyM_Lq82MKO_tGK9uERx1CNhfla2uIdW1Wj9XC..; isg=BISEcFN_vdkGtwkVVcJv6Qa6VQJ2nagHzb-qWZ4kB88SySSTxq3Bl_RnCWERUeBf',
        'Referer':'https://dianying.taobao.com/?spm=a1z21.3046609.city.1.69db112aYQ7Kd2&city=110100',
        'Sec-Ch-Ua':'"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"',
        'Sec-Ch-Ua-Mobile':'?0',
        'Sec-Ch-Ua-Platform':'"Windows"',
        'Sec-Fetch-Dest':'empty',
        'Sec-Fetch-Mode':'cors',
        'Sec-Fetch-Site':'same-origin',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
        'X-Requested-With':'XMLHttpRequest',
    }
    request = urllib.request.Request(url = url,headers = headers)
    response = urllib.request.urlopen(request)
    com = response.read().decode('utf-8')
    com = com.split('(')[1].split(')')[0]

    fs = open('../../爬取的文件/淘票票.json', 'w', encoding='utf-8')
    fs.write(com)
    # print(com)
    obj = json.load(open('../../爬取的文件/淘票票.json', 'r', encoding='utf-8'))
    city_list = jsonpath.jsonpath(obj,'$..regionName')
    print(city_list)
